package org.jingouzhui.domain.strategy.service.rule.tree;

import lombok.extern.slf4j.Slf4j;
import org.jingouzhui.domain.strategy.model.valboj.RuleLogicCheckTypeVO;
import org.jingouzhui.domain.strategy.repository.IStrategyRepository;
import org.jingouzhui.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import org.jingouzhui.types.exception.AppException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @description: 限定抽奖n次后才能解锁
 * @author: jingouzhui
 * @date: 2025/5/1 16:43
 */
@Slf4j
@Component("rule_lock")
public class RuleLockLogicTreeNode implements ILogicTreeNode {

    @Resource
    private IStrategyRepository strategyRepository;

    private final Long useRaffleCount = 10L;

    @Override
    public DefaultTreeFactory.TreeActionEntity logic(String userId, Long strategyId, Integer awardId, String ruleValue) {
        log.info("规则树-次数锁限定抽奖n次后才能解锁流程开始,userId:{},strategyId:{},awardId:{}", userId, strategyId, awardId);
        Long raffleCount = 0L;
        try {
            raffleCount = Long.parseLong(ruleValue);
        } catch (NumberFormatException e) {
            throw new AppException("限定n次后才能抽奖的配置有误", e);
        }
        if (useRaffleCount >= raffleCount) {
            log.info("规则树-次数锁限定抽奖n次后才能解锁--可以放行,userId:{},strategyId:{},awardId:{}", userId, strategyId, awardId);
            return DefaultTreeFactory.TreeActionEntity.builder()
                    .ruleLogicCheckTypeVO(RuleLogicCheckTypeVO.ALLOW)
                    .strategyAwardVO(DefaultTreeFactory.StrategyAwardVO.builder()
                            .awardId(awardId)
                            .awardRuleValue(ruleValue)
                            .build())
                    .build();
        }
        log.info("规则树-次数锁限定抽奖n次后才能解锁--接管,userId:{},strategyId:{},awardId:{}", userId, strategyId, awardId);
        return DefaultTreeFactory.TreeActionEntity.builder()
                .ruleLogicCheckTypeVO(RuleLogicCheckTypeVO.TAKE_OVER)
                .build();
    }
}
